/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands;

import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.commands.Command;

import com.cea.papyrus.diagraminterchange2.di2.GraphNode;


/**
 * This command is used to delete a Graphic element from a layout.
 */
public class OrphanChildCommand extends Command {

	/**
	 * the graph node which is the container of the graph n ode to remove.
	 */
	private GraphNode parent;
	
	/**
	 * the graph node to remove.
	 */
	private GraphNode child;

	/**
	 * keeps index of the removed children in the list of contained elements.
	 */
	private int index = -1;


	/**
	 * Creates a new OrphanChildCommand.
	 */
	public OrphanChildCommand () {
		super();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	/**
	 * 
	 */
	@Override
	public void execute() {
		EList children = parent.getContained();
		index = children.indexOf(child);
		children.remove(child);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	/**
	 * 
	 */
	@Override
	public void redo() {
		parent.getContained().remove(child);
	}

	/**
	 * Sets the graph element to remove.
	 * 
	 * @param child the graph node to remove
	 */
	public void setChild(GraphNode child) {
		this.child = child;
	}

	/**
	 * Sets the graph element container of the element to be removed.
	 * 
	 * @param parent the graph element container of the element to be removed
	 */
	public void setParent(GraphNode parent) { 
		this.parent = parent;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	/**
	 * 
	 */
	@Override
	public void undo() {
		// check if it does not already exists in the list
		if(-1 == parent.getContained().indexOf(child)) {
			parent.getContained().add(index, child);	
		}
	}
}
